package com.zprmb.udzt;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.umeng.analytics.MobclickAgent;
import com.zprmb.udzt.activity.FailYueActivity;
import com.zprmb.udzt.activity.SuccessYueActivity;
import com.zprmb.udzt.bean.YuePaySuccess;
import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.CustomDialog;
import com.zprmb.udzt.util.CustomDialog_view;
import com.zprmb.udzt.util.KeyboardEnum;
import com.zprmb.udzt.util.KeyboardEnum.ActionEnum;
import com.zprmb.udzt.util.ToastUtil;
import com.zprmb.udzt.util.VolleryUtil;
import com.zprmb.udzt.util.VolleryUtil.OnButtonListener;
import com.zprmb.udzt.view.MdStyleProgress;
import com.zprmb.udzt.view.MdStyleProgress.OnDoneCircleAnimListner;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 自定义密码键盘
 *
 * @author shenshao
 */
public class PayActivity extends BaseActivity implements OnClickListener, OnButtonListener {

    private TextView pay_keyboard_one;
    private TextView pay_keyboard_two;
    private TextView pay_keyboard_three;
    private TextView pay_keyboard_four;
    private TextView pay_keyboard_five;
    private TextView pay_keyboard_sex;
    private TextView pay_keyboard_seven;
    private TextView pay_keyboard_eight;
    private TextView pay_keyboard_nine;
    private TextView pay_keyboard_zero;
    private TextView pay_keyboard_del;
    private LinearLayout li_keyboard;
    private LinearLayout rl_2;
    private TextView tv_v4_pay_next;
    private TextView tv_v4_pay_zhifu;
    private ArrayList<String> mList = new ArrayList<String>();
    private StringBuffer sb = new StringBuffer();
    private StringBuffer sb_money = new StringBuffer();
    private StringBuffer sb_set_pass_1 = new StringBuffer();
    private StringBuffer sb_set_pass_2 = new StringBuffer();
    private TextView pay_box1;
    private TextView pay_box2;
    private TextView pay_box3;
    private TextView pay_box4;
    private TextView pay_box5;
    private TextView pay_box6;
    private String set_pass_count = "one";
    private ImageView iv_v4_pay_finish;
    //关闭布局
    private LinearLayout layout_guanbi;
    private String firstType = "";
    private String flagFresh;
    String firstamount = "";
    private int who = 1;
    private double availableGiftAmount;// 可用红包
    private double availableAmount;// 可用余额
    private String productId, money;
    private boolean flagRedpack, flagcoupon;// 是否可以使用红包\
    private double rate;
    private int qixian;
    private String title_tpye;
    private int minAmount;
    private int setPass;// 设置交易密码状态 1为未设置
    private int openfingerPay;
    private String pass1;// 设置交易密码状态下
    private CheckBox checkBox1;
    private TextView tv_v4_pay_paypass_status;
    private LinearLayout ll_v4_pay_redpackage;// 是否勾选红包界面
    private int isZhi;// 1为直投
    private int increaseNum;// 投资增量
    private int remainAmount;// 剩余可投，直投
    private LinearLayout ll_v4_pay_redpackage_fanxian;// 红包返现界面
    private TextView tv_v4_pay_forgetpassword;
    private int maxAmount;//新手标独有最大投标数属性
    private boolean isNewUesr, ishuo;//是否新手
    private String raisingratesId, red_id;//加息券id
    private SharedPreferences sha;
    Animation anim;
    private MdStyleProgress invest_progress;
    private CustomDialog.Builder mDialog;

    String intentmoney;
    TextView text_shibai;
    View view_touming;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.v4_pay_activity);

        BbtApplication.getInstance().addActvity(this);
        WindowManager.LayoutParams layoutparams = getWindow().getAttributes();
        layoutparams.width = layoutparams.MATCH_PARENT;
        getWindow().setAttributes(layoutparams);
        view_touming = findViewById(R.id.view_touming);
        view_touming.setBackground(getResources().getDrawable(R.color.transparent));
        text_shibai = (TextView) findViewById(R.id.text_shibai);

        anim = AnimationUtils.loadAnimation(PayActivity.this,
                R.anim.disappear_bottom_right_in);
        Intent intent = getIntent();
        sha = getSharedPreferences("coupon", MODE_PRIVATE);
        productId = intent.getStringExtra("productId");
        title_tpye = intent.getStringExtra("title_tpye");
//        Toast.makeText(this, title_tpye+"", Toast.LENGTH_SHORT).show();

        flagRedpack = intent.getBooleanExtra("flagRedpack", false);
        flagcoupon = intent.getBooleanExtra("flagcoupon", false);
        flagFresh = intent.getStringExtra("flagFresh");
        rate = intent.getDoubleExtra("rate", 0);
        qixian = intent.getIntExtra("qixian", 0);
        minAmount = intent.getIntExtra("minAmount", 0);

//		Toast.makeText(this,minAmount+"", Toast.LENGTH_SHORT).show();


        setPass = intent.getIntExtra("setPass", 0);

        openfingerPay = intent.getIntExtra("openfingerPay", 0);
//		  animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, toXDelta, fromYDelta, toYDelta)
//	        animation.setDuration(150);  
//	        animation.setRepeatCount(1);//动画的重复次数  
//	        animation.setFillAfter(false);//设置为true，动画转化结束后被应用  

        ishuo = getIntent().getBooleanExtra("useCurrentAmountToInvest", false);
        isZhi = intent.getIntExtra("isZhi", 0);
        money = intent.getStringExtra("money");


//        Toast.makeText(this, getIntent().getStringExtra("productId") + "", Toast.LENGTH_SHORT).show();
//        Toast.makeText(this, getIntent().getStringExtra("cardId") + "", Toast.LENGTH_SHORT).show();
//        Toast.makeText(this, money + "", Toast.LENGTH_SHORT).show();
        raisingratesId = intent.getStringExtra("raisingratesId");
        red_id = getIntent().getStringExtra("red_id");
        increaseNum = intent.getIntExtra("increaseNum", 0);
        remainAmount = intent.getIntExtra("remainAmount", 0);
        maxAmount = intent.getIntExtra("maxAmount", 0);
        isNewUesr = intent.getBooleanExtra("isNewUesr", false);

        initView();
//        Toast.makeText(PayActivity.this, red_id, Toast.LENGTH_SHORT).show();
    }

    private CustomDialog_view.Builder builder;


    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if (sha != null) {
            Editor editor = sha.edit();
            editor.clear();
            editor.commit();
        }
        BbtApplication.getInstance().removeActvity(this);
    }

    private void initView() {


        // et_pay_money.setInputType(InputType.TYPE_NULL);
        invest_progress = (MdStyleProgress) findViewById(R.id.invest_progress);
        li_keyboard = (LinearLayout) findViewById(R.id.li_keyboard);
        pay_keyboard_one = (TextView) findViewById(R.id.pay_keyboard_one);
        pay_keyboard_two = (TextView) findViewById(R.id.pay_keyboard_two);
        pay_keyboard_three = (TextView) findViewById(R.id.pay_keyboard_three);
        pay_keyboard_four = (TextView) findViewById(R.id.pay_keyboard_four);
        pay_keyboard_five = (TextView) findViewById(R.id.pay_keyboard_five);
        pay_keyboard_sex = (TextView) findViewById(R.id.pay_keyboard_sex);
        pay_keyboard_seven = (TextView) findViewById(R.id.pay_keyboard_seven);
        pay_keyboard_eight = (TextView) findViewById(R.id.pay_keyboard_eight);
        pay_keyboard_nine = (TextView) findViewById(R.id.pay_keyboard_nine);
        pay_keyboard_zero = (TextView) findViewById(R.id.pay_keyboard_zero);
        pay_keyboard_del = (TextView) findViewById(R.id.pay_keyboard_del);
        tv_v4_pay_paypass_status = (TextView) findViewById(R.id.tv_v4_pay_paypass_status);
        tv_v4_pay_forgetpassword = (TextView) findViewById(R.id.tv_v4_pay_forgetpassword);
        tv_v4_pay_forgetpassword.setOnClickListener(this);
        pay_keyboard_one.setOnClickListener(this);
        pay_keyboard_two.setOnClickListener(this);
        pay_keyboard_three.setOnClickListener(this);
        pay_keyboard_four.setOnClickListener(this);
        pay_keyboard_five.setOnClickListener(this);
        pay_keyboard_sex.setOnClickListener(this);
        pay_keyboard_seven.setOnClickListener(this);
        pay_keyboard_eight.setOnClickListener(this);
        pay_keyboard_nine.setOnClickListener(this);
        pay_keyboard_zero.setOnClickListener(this);
        pay_keyboard_del.setOnClickListener(this);

        rl_2 = (LinearLayout) findViewById(R.id.rl_2);

        tv_v4_pay_zhifu = (TextView) findViewById(R.id.tv_v4_pay_zhifu);

        tv_v4_pay_zhifu.setOnClickListener(this);
        pay_box1 = (TextView) findViewById(R.id.pay_box1);
        pay_box2 = (TextView) findViewById(R.id.pay_box2);
        pay_box3 = (TextView) findViewById(R.id.pay_box3);
        pay_box4 = (TextView) findViewById(R.id.pay_box4);
        pay_box5 = (TextView) findViewById(R.id.pay_box5);
        pay_box6 = (TextView) findViewById(R.id.pay_box6);
        iv_v4_pay_finish = (ImageView) findViewById(R.id.iv_v4_pay_finish);
        layout_guanbi = (LinearLayout) findViewById(R.id.layout_guanbi);
        layout_guanbi.setOnClickListener(this);
        iv_v4_pay_finish.setOnClickListener(this);
        if (setPass == 1) {
            tv_v4_pay_forgetpassword.setVisibility(View.GONE);
            tv_v4_pay_paypass_status.setText("请设置交易密码");
        }


    }

    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.tv_v4_pay_zhifu:
                // ToastUtil.showToast(this, mList.toString());
                sb.delete(0, sb.length());
                for (int i = 0; i < mList.size(); i++) {
                    sb.append(mList.get(i));
                }
                if (sb.length() < 6) {
                    ToastUtil.showCenterToast(this, "交易密码必须为6位");
                    return;
                }
                if (tv_v4_pay_zhifu.getText().toString().equals("确认")) {
                    tv_v4_pay_zhifu.setEnabled(false);

                    invest();
                }
                break;
            case R.id.iv_v4_pay_finish:
                finish();
                break;
            case R.id.layout_guanbi:

                finish();

                break;

            case R.id.pay_keyboard_one:
                //et_pay_money.setSelection(sb_money.length());
                if (setPass == 1) {
                    setPayPass(KeyboardEnum.one);
                } else {

                    parseActionType(KeyboardEnum.one);
                }

                break;
            case R.id.pay_keyboard_two:
                //et_pay_money.setSelection(sb_money.length());
                if (setPass == 1) {
                    setPayPass(KeyboardEnum.two);
                } else {

                    parseActionType(KeyboardEnum.two);
                }


                break;
            case R.id.pay_keyboard_three:
                //et_pay_money.setSelection(sb_money.length());

                if (setPass == 1) {
                    setPayPass(KeyboardEnum.three);
                } else {

                    parseActionType(KeyboardEnum.three);
                }


                break;
            case R.id.pay_keyboard_four:


                if (setPass == 1) {
                    setPayPass(KeyboardEnum.four);
                } else {

                    parseActionType(KeyboardEnum.four);
                }

                break;
            case R.id.pay_keyboard_five:

                if (setPass == 1) {
                    setPayPass(KeyboardEnum.five);
                } else {

                    parseActionType(KeyboardEnum.five);
                }

                break;
            case R.id.pay_keyboard_sex:

                if (setPass == 1) {
                    setPayPass(KeyboardEnum.sex);
                } else {

                    parseActionType(KeyboardEnum.sex);
                }

                break;
            case R.id.pay_keyboard_seven:

                if (setPass == 1) {
                    setPayPass(KeyboardEnum.seven);
                } else {

                    parseActionType(KeyboardEnum.seven);
                }

                break;
            case R.id.pay_keyboard_eight:

                if (setPass == 1) {
                    setPayPass(KeyboardEnum.eight);
                } else {

                    parseActionType(KeyboardEnum.eight);
                }

                break;
            case R.id.pay_keyboard_nine:

                if (setPass == 1) {
                    setPayPass(KeyboardEnum.nine);
                } else {

                    parseActionType(KeyboardEnum.nine);
                }

                break;
            case R.id.pay_keyboard_zero:

                if (setPass == 1) {
                    setPayPass(KeyboardEnum.zero);
                } else {

                    parseActionType(KeyboardEnum.zero);
                }

                break;
            case R.id.pay_keyboard_del:

                if (setPass == 1) {
                    setPayPass(KeyboardEnum.del);
                } else {

                    parseActionType(KeyboardEnum.del);
                }

                break;
            case R.id.tv_v4_pay_forgetpassword:
                Intent intent = new Intent(this, Reset_pwd_Mes.class);
                startActivity(intent);

                break;
        }
    }

    /**
     * 设置交易密码
     *
     * @param type
     */
    private void setPayPass(KeyboardEnum type) {
        if (type.getType() == ActionEnum.add) {
            if (sb_set_pass_1.length() < 6) {
                mList.add(type.getValue());
                sb_set_pass_1.append(type.getValue());
                updateUi();
                if (mList.size() == 6) {
                    sb_set_pass_1.delete(0, sb_set_pass_1.length());
                    for (int i = 0; i < mList.size(); i++) {
                        sb_set_pass_1.append(mList.get(i));
                    }
                    if (tv_v4_pay_paypass_status.getText().toString().equals("请设置交易密码")) {
                        tv_v4_pay_paypass_status.setText("请确认交易密码");
                        pass1 = sb_set_pass_1.toString();
                        sb_set_pass_1.delete(0, sb_set_pass_1.length());
                        mList.clear();
                        updateUi();
                        li_keyboard.setAnimation(anim);
                    } else if (tv_v4_pay_paypass_status.getText().toString().equals("请确认交易密码")) {
                        if (pass1.equals(sb_set_pass_1.toString())) {
                            initSetPass();
//							ToastUtil.showCenterToast(this, "设置密码");
                        } else {
                            ToastUtil.showCenterToast(this, "两次输入的交易密码不一致，请重新输入");
                        }
                    }


                }

            }
        } else if (type.getType() == ActionEnum.delete) {
            if (sb_set_pass_1.length() > 0) {
                mList.remove(mList.get(mList.size() - 1));
                sb_set_pass_1.delete(sb_set_pass_1.length() - 1,
                        sb_set_pass_1.length());
                updateUi();
            }
        }
//		Log.e("list长度", mList.size()+"");
//		Log.e("sb_set_pass长度", sb_set_pass_1.length()+"");
    }

    protected void onStop() {
        super.onStop();

    }


    private void parseActionType(KeyboardEnum type) {
        if (type.getType() == ActionEnum.add) {
            if (mList.size() < 6) {
                mList.add(type.getValue());
                updateUi();
                if (mList.size() == 6) {
                    sb.delete(0, sb.length());
                    for (int i = 0; i < mList.size(); i++) {
                        sb.append(mList.get(i));
                    }
                    li_keyboard.setVisibility(View.GONE);
                    invest_progress.setVisibility(View.VISIBLE);
                    text_shibai.setVisibility(View.GONE);
                    invest();

                }
            } else {

            }
        } else if (type.getType() == ActionEnum.delete) {
            if (mList.size() > 0) {
                mList.remove(mList.get(mList.size() - 1));
                updateUi();
            }
        } else if (type.getType() == ActionEnum.cancel) {
            // listener.onCancelPay();
        } else if (type.getType() == ActionEnum.sure) {

        } else if (type.getType() == ActionEnum.longClick) {

        }
    }

    private void updateUi() {
        if (mList.size() == 0) {
            pay_box1.setText("");
            pay_box2.setText("");
            pay_box3.setText("");
            pay_box4.setText("");
            pay_box5.setText("");
            pay_box6.setText("");
        } else if (mList.size() == 1) {
            pay_box1.setText(mList.get(0));
            pay_box2.setText("");
            pay_box3.setText("");
            pay_box4.setText("");
            pay_box5.setText("");
            pay_box6.setText("");
        } else if (mList.size() == 2) {
            pay_box1.setText(mList.get(0));
            pay_box2.setText(mList.get(1));
            pay_box3.setText("");
            pay_box4.setText("");
            pay_box5.setText("");
            pay_box6.setText("");
        } else if (mList.size() == 3) {
            pay_box1.setText(mList.get(0));
            pay_box2.setText(mList.get(1));
            pay_box3.setText(mList.get(2));
            pay_box4.setText("");
            pay_box5.setText("");
            pay_box6.setText("");
        } else if (mList.size() == 4) {
            pay_box1.setText(mList.get(0));
            pay_box2.setText(mList.get(1));
            pay_box3.setText(mList.get(2));
            pay_box4.setText(mList.get(3));
            pay_box5.setText("");
            pay_box6.setText("");
        } else if (mList.size() == 5) {
            pay_box1.setText(mList.get(0));
            pay_box2.setText(mList.get(1));
            pay_box3.setText(mList.get(2));
            pay_box4.setText(mList.get(3));
            pay_box5.setText(mList.get(4));
            pay_box6.setText("");
        } else if (mList.size() == 6) {
            pay_box1.setText(mList.get(0));
            pay_box2.setText(mList.get(1));
            pay_box3.setText(mList.get(2));
            pay_box4.setText(mList.get(3));
            pay_box5.setText(mList.get(4));
            pay_box6.setText(mList.get(5));
        }
    }

    private final String ACTION_NAME = "发送广播";

    private final String ACTION_NAME2 = "详情页面的跳转";

    /**
     * 投资
     */
    private void invest() {
        AjaxParams params = new AjaxParams();
        if (isZhi == 1) {
            params.put("productType", title_tpye);
            params.put("loanId", productId);
            params.put("amount", money);
            params.put("payPass", sb.toString());
            params.put("useCurrentAmountToInvest", ishuo + "");
            params.put("payType", "PWD");
            if (flagRedpack) {

                params.put("giftId", red_id);

            }
//			else {
//				params.put("giftId", "off");
//			}
        } else {
            params.put("productType", title_tpye);
            params.put("productId", productId);
     /*       if (TextUtils.isEmpty(getIntent().getStringExtra("myflag"))) {
                params.put("giftId", red_id);
            }*/
            if (getIntent().getStringExtra("myflag").equals("1")) {
                params.put("giftId", red_id);
            } else if (getIntent().getStringExtra("myflag").equals("2")) {
                params.put("cardId", getIntent().getStringExtra("cardId"));
            } else {
                params.put("raisingratesId", raisingratesId);
            }


            params.put("amount", money);
            params.put("payType", "PWD");
            params.put("useCurrentAmountToInvest", ishuo + "");
            params.put("payPass", sb.toString());
          /*  if (flagRedpack) {
                params.put("giftId", red_id);
            }
//			else {
//				params.put("giftId", "off");
//			}
            Log.e("传到加息券   ", flagcoupon + "");
            if (flagcoupon) {

                params.put("raisingratesId", raisingratesId);
            }*/
            Log.e("投资", params.toString());

        }
        VolleryUtil vollery = new VolleryUtil() {

            @Override
            public void onUpDate(final JSONObject jsonObject) {
                // TODO Auto-generated method stub
                Log.e("invest", jsonObject.toString());
                if (jsonObject.optInt("success") == 1) {
                    Editor editor2 = getShare().edit();
                    editor2.putInt("invest_num", getInvestNum() + 1);
                    editor2.commit();
                    clearCoupon();
                    invest_progress.setStatus(MdStyleProgress.Status.LoadSuccess);
                    invest_progress.startAnima();
                    tv_v4_pay_zhifu.setEnabled(true);
                    Log.e("invest", jsonObject.toString());
          /*          ToastUtil.showCenterToast(PayActivity.this,
                            jsonObject.optString("comment"));*/
                    final JSONObject json1 = jsonObject.optJSONObject("data");

                    firstType = json1.optString("firstinvestType");
                    firstamount = json1.optString("ExperienceAmount");
                    if (json1.optBoolean("firstFlag")) {
                        firstType = "1";
                    } else {
                        firstType = "0";
                    }
//					Log.e("首投", firstType+""+json1.optDouble("ExperienceAmount"));
//					ToJSON.initData(jsonObject.toString());
                    invest_progress.addCircleAnimatorEndListner(new OnDoneCircleAnimListner() {

                        @Override
                        public void onCircleDone() {
                            // TODO Auto-generated method stub


                            YuePaySuccess yuePaySuccess = new YuePaySuccess();
                            Gson gson = new Gson();
                            yuePaySuccess = gson.fromJson(jsonObject.toString(), YuePaySuccess.class);


                            Intent intent = new Intent(PayActivity.this, SuccessYueActivity.class);

                            intent.putExtra("active_url", yuePaySuccess.getData().getExpUrl());

                            intent.putExtra("title", getIntent().getStringExtra("title"));

                            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//收益到账
                            Long time = new Long(yuePaySuccess.getData().getExpectDueDate());
                            //起息日期
                            Long time2 = new Long(yuePaySuccess.getData().getCount_rate_date());
                            String d = format.format(time);
                            String e = format.format(time2);
                            intent.putExtra("qixi", e);

                            intent.putExtra("jieshu", d);
                            intent.putExtra("investmoney", yuePaySuccess.getData().getAmount());

                            startActivity(intent);
                            Intent mIntent = new Intent(ACTION_NAME);
                            mIntent.putExtra("yaner", "发送广播，相当于在这里传送数据");
                            //发送广播
                            sendBroadcast(mIntent);

                            Intent mIntent2 = new Intent(ACTION_NAME2);
                            mIntent2.putExtra("yaner", "发送广播，相当于在这里传送数据");
                            //发送广播
                            sendBroadcast(mIntent2);

                            finish();
                            /*if (isZhi == 1) {

                                YuePaySuccess yuePaySuccess = new YuePaySuccess();
                                Gson gson = new Gson();
                                yuePaySuccess = gson.fromJson(jsonObject.toString(), YuePaySuccess.class);


                                Intent intent = new Intent(PayActivity.this, SuccessYueActivity.class);

                                intent.putExtra("active_url", yuePaySuccess.getData().getExpUrl());

                                intent.putExtra("title", getIntent().getStringExtra("title"));

                                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//收益到账
                                Long time = new Long(yuePaySuccess.getData().getExpectDueDate());
                                //起息日期
                                Long time2 = new Long(yuePaySuccess.getData().getCount_rate_date());
                                String d = format.format(time);
                                String e = format.format(time2);
                                intent.putExtra("qixi", e);

                                intent.putExtra("jieshu", d);
                                intent.putExtra("investmoney", yuePaySuccess.getData().getAmount());

                                startActivity(intent);
                                Intent mIntent = new Intent(ACTION_NAME);
                                mIntent.putExtra("yaner", "发送广播，相当于在这里传送数据");
                                //发送广播
                                sendBroadcast(mIntent);
                                finish();

                            } else {


                            }*/
                        }
                    });

                    if (isNewUesr) {
                        Editor editor = getShare().edit();
                        editor.putBoolean("isShowNewUserRedPackage", true);
                        editor.commit();
                    }
                    UmengDate();
                } else {
                 /*   ToastUtil.showCenterToast(PayActivity.this,
                            jsonObject.optString("comment"));*/

                    if (jsonObject.optString("comment").contains("交易密码错误")) {
                      /*  ToastUtil.showCenterToast(PayActivity.this,
                                jsonObject.optString("comment"));*/
                        text_shibai.setVisibility(View.VISIBLE);
                        text_shibai.setText(jsonObject.optString("comment"));
                        invest_progress.setVisibility(View.GONE);
                        li_keyboard.setVisibility(View.VISIBLE);
                        mList.clear();
                        pay_box1.setText("");
                        pay_box2.setText("");
                        pay_box3.setText("");
                        pay_box4.setText("");
                        pay_box5.setText("");
                        pay_box6.setText("");
                    } else {

                        Intent intent2 = new Intent(PayActivity.this, FailYueActivity.class);
                        intent2.putExtra("comment", jsonObject.optString("comment"));
                        startActivity(intent2);
                        Intent mIntent = new Intent(ACTION_NAME);
                        mIntent.putExtra("yaner", "发送广播，相当于在这里传送数据");
                        //发送广播
                        sendBroadcast(mIntent);

                        Intent mIntent2 = new Intent(ACTION_NAME2);
                        mIntent2.putExtra("yaner", "发送广播，相当于在这里传送数据");
                        //发送广播
                        sendBroadcast(mIntent2);
                        finish();
                    }


                  /*  invest_progress.setStatus(MdStyleProgress.Status.LoadFail);
                    invest_progress.failAnima();
                    invest_progress.addCircleAnimatorEndListner(new OnDoneCircleAnimListner() {

                        @Override
                        public void onCircleDone() {
                            // TODO Auto-generated method stub
                            finish();
                        }
                    });*/
                    if (jsonObject.optString("comment").equals(
                            "please login")) {
                        if (jsonObject.optString("comment").equals("please login")) {
                            Intent intent = new Intent(PayActivity.this,
                                    LoginActivity.class);
                            intent.putExtra("close", 1);
                            startActivity(intent);
                        }
                    }

                    tv_v4_pay_zhifu.setEnabled(true);
                }
            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub
                Intent intent = new Intent(PayActivity.this, V6ResultFinsh.class);
                intent.putExtra("title", "投资结果");
                if (isZhi == 1) {
                    intent.putExtra("description1", "您好，系统正在处理中，\n请3~5分钟之后前往 “我的账户-散标投资”");

                } else {
                    intent.putExtra("description1", "您好，系统正在处理中，\n请3~5分钟之后前往 “我的账户-定期计划”");
                }
                intent.putExtra("description2", "确认投资结果！若投资失败请重新操作");
                intent.putExtra("button", "我的账户");
                startActivity(intent);
                PayActivity.this.finish();


            }
        };
        vollery.OnButtonListener(this);
        vollery.setShow(true);
        vollery.setRequestMethod(1);
        vollery.setHeaders("token", getToken());
        vollery.setHeaders("deviceId", getdeviceId());
        vollery.setHeaders("client", "android");
        if (isZhi == 1) {
            vollery.execute(this,
                    Constant.URL + "apiInvest/investProduct?",
                    params.toString());

        } else {
            vollery.execute(this, Constant.URL
                    + "api/v6/invest/invest?", params.toString());

        }
    }

    @Override
    protected void onActivityResult(int arg0, int arg1, Intent arg2) {
        // TODO Auto-generated method stub
        super.onActivityResult(arg0, arg1, arg2);

        if (arg1 == 1) {
            Log.e("payresult", arg0 + "");
            Intent intent = new Intent();
            setResult(3, intent);
            finish();
        }
    }


    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            overridePendingTransition(R.anim.slide_in_from_bottom,
                    R.anim.slide_out_to_bottom);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 设置交易密码
     */
    private void initSetPass() {
        AjaxParams params = new AjaxParams();
        params.put("payPass", sb_set_pass_1.toString());
        VolleryUtil volleryUtil = new VolleryUtil() {

            @Override
            public void onUpDate(JSONObject jsonObject) {
                // TODO Auto-generated method stub


                if (jsonObject.optInt("success") == 1) {
                    tv_v4_pay_paypass_status.setText("请输入交易密码");
                    // setPassStatus = 1;
                    mList.clear();
                    updateUi();
                    li_keyboard.setAnimation(anim);
//						tv_v4_pay_zhifu.setText("投资");
                    ToastUtil.showCenterToast(PayActivity.this,
                            jsonObject.optString("comment"));
//						rl_2.setVisibility(View.GONE);
//						rl_1.setVisibility(View.VISIBLE);
                    tv_v4_pay_forgetpassword.setVisibility(View.VISIBLE);
                    who = 1;
                    setPass = 0;
                    if (getInvestNum() == 0 & getisFinger() == 1 & getisFingerPay() == 0) {
                        showDialog();

                        return;
                    }
                } else {
                    ToastUtil.showCenterToast(PayActivity.this,
                            jsonObject.optString("comment"));
                }

            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub

            }
        };
        volleryUtil.setHeaders("token", getToken());
        volleryUtil.setHeaders("client", "android");
        volleryUtil.setShow(true);
        volleryUtil.execute(this, Constant.URL + "api/v2/setPayPass?",
                params.toString());
    }


    private void UmengDate() {
        try {
            ApplicationInfo appInfo = this.getPackageManager()
                    .getApplicationInfo(getPackageName(),
                            PackageManager.GET_META_DATA);
            String msg = appInfo.metaData.getString("UMENG_CHANNEL");
            Map<String, String> m = new HashMap<String, String>();
            m.put(msg, msg);
            MobclickAgent.onEventValue(this, "investA", m,
                    Integer.valueOf(money));
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void showDialog() {
        mDialog = new CustomDialog.Builder(PayActivity.this);

        mDialog.setMessage("优点智投上线指纹支付功能，是否开启使用？");
        mDialog.create().setCancelable(false);
        mDialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                mDialog.create().dismiss();
            }
        });
        mDialog.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                mStartActivity(PayActivity.this, V6FingerPayPwdConfirmActivity.class);
                mDialog.create().dismiss();
                finish();
            }
        });
        mDialog.create().show();
    }

    @Override
    public void change() {
        // TODO Auto-generated method stub
        tv_v4_pay_zhifu.setEnabled(true);
    }

}
